package tech.xs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.fastjson.JSONObject;

import tech.xs.entity.Department;
import tech.xs.service.DepartmentService;
import tech.xs.service.impl.DepartmentServiceImpl;
@WebServlet("/department")
@SuppressWarnings("serial")
public class DepartmentMapperServlet extends HttpServlet {
	@SuppressWarnings("resource")
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json;charset=UTF-8");
		ApplicationContext as= new ClassPathXmlApplicationContext("applicationContext.xml");
		DepartmentService departmentService=(DepartmentService) as.getBean("departmentServiceImpl");
		PrintWriter io= resp.getWriter();
		JSONObject json=new JSONObject();
		String opr= req.getParameter("opr");
		switch (opr) {
		case "query":
			Integer page=Integer.parseInt(req.getParameter("page")) ;
			Integer limit=Integer.parseInt(req.getParameter("limit"));
			List<Department> list=departmentService.getlist(page, limit);
			Integer count=departmentService.getCount();
			json.put("code", 0);
			json.put("count", count);
			json.put("data", list);
			io.append(json.toString());
			io.flush();
			io.close();
			break;
		case "add":
			String dname= req.getParameter("dname");
			String dloc= req.getParameter("dloc");
			Department d=new Department();
			d.setDname(dname);
			d.setDloc(dloc);
			boolean b= departmentService.Add(d);
			if (b) {
				json.put("success", true);
			} else {
				json.put("success", false);
			}
			io.append(json.toString());
			io.flush();
			io.close();
			break;
		case "update":
			Integer id2=Integer.parseInt(req.getParameter("id")) ;
			String dname2= req.getParameter("dname");
			String dloc2= req.getParameter("dloc");
			Department d2=new Department();
			d2.setId(id2);
			d2.setDname(dname2);
			d2.setDloc(dloc2);
			boolean b2= departmentService.Amend(d2);
			if (b2) {
				json.put("success", true);
			} else {
				json.put("success", false);
			}
			io.append(json.toString());
			io.flush();
			io.close();
			break;
		case "delete":
			Integer id3=Integer.parseInt(req.getParameter("id")) ;
			boolean b3= departmentService.delete(id3);
			if (b3) {
				json.put("success", true);
			} else {
				json.put("success", false);
			}
			io.append(json.toString());
			io.flush();
			io.close();
			break;
		case "selectId":
			Integer id4=Integer.parseInt(req.getParameter("id")) ;
			List<Department> list2=departmentService.getSelectId(id4);
			json.put("data", list2);
			io.append(json.toString());
			io.flush();
			io.close();
			break;
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
